import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_task_instructions_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_task_process_nodes_entity.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';

class DrillProcessController extends GetxController {
  /// 演练计划id
  String? exercisePlanId;

  /// 演练流程数据
  List<EmergencyTaskProcessNodesEntity> nodeProcessList = [];

  /// 搜索字符串
  String? search;

  /// 初始化数据
  void initData(String? exercisePlanId) {
    this.exercisePlanId = exercisePlanId;
    getExercisePlanList();
  }

  /// 获取演练流程数据
  void getExercisePlanList() async {
    // 获取流程列表接口
    final res = await HttpUtil().generateGetRequest<List<EmergencyTaskProcessNodesEntity>>(
      Interface.getNodeByBusinessId,
      queryParameters: {
        'id': exercisePlanId,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        nodeProcessList = result.data!;
        // 剔除开始和结束节点
        nodeProcessList = nodeProcessList.where((element) => element.nodeType != 3 && element.nodeType != 4).toList();
        // 判断是否全选
        for (var node in nodeProcessList) {
          node.isSelected = isAllSelect(node);
        }
      }
      update();
    });
  }

  /// 选择外层父类任务
  void selectTaskView(EmergencyTaskProcessNodesEntity bean) {
    // 反选
    bean.isSelected = !bean.isSelected;
    // 如果选中则设置子类所有都选中
    if (bean.isSelected) {
      for (var item in bean.emergencyTaskInstructions) {
        setNodeState(item, true);
      }
    } else {
      // 如果未选中则设置子类所有都未选中
      for (var item in bean.emergencyTaskInstructions) {
        setNodeState(item, false);
      }
    }
    update();
  }

  /// 选择节点
  /// [parentBean]父节点数据
  void selectNodeView(EmergencyTaskProcessNodesEntity parentBean, EmergencyTaskInstructionsEntity bean) {
    // 反选
    setNodeState(bean, !(bean.whetherToExecute == '1'));

    // 判断父节点反选逻辑
    if (parentBean.isSelected && !(bean.whetherToExecute == '1')) {
      parentBean.isSelected = false;
    } else {
      // 是否全部选中
      parentBean.isSelected = isAllSelect(parentBean);
    }
    update();
  }

  /// 判断是否全部选中
  bool isAllSelect(EmergencyTaskProcessNodesEntity parentBean) {
    // 是否全部选中
    bool isAll = true;
    for (var item in parentBean.emergencyTaskInstructions) {
      // 如果有未选中的
      if (!(item.whetherToExecute == '1')) {
        isAll = false;
        break;
      }
    }
    return isAll;
  }

  /// 设置子类节点选中状态,修改执行时间
  /// [bean] 节点数据
  /// [isSelect]  是否选中
  void setNodeState(EmergencyTaskInstructionsEntity bean, bool isSelect) {
    if (bean.whetherToExecute == '1' && isSelect) {
      return;
    }
    if (bean.whetherToExecute != '1' && !isSelect) {
      return;
    }
    bean.whetherToExecute = isSelect ? '1' : '0';
    // 如果是选中状态,设置执行时间为当前时间
    if (bean.whetherToExecute == '1') {
      bean.updateDate = Jiffy.parseFromDateTime(DateTime.now()).format(pattern: 'yyyy-MM-dd HH:mm:ss');
    } else {
      bean.updateDate = '';
    }
    // 修改任务指令
    updateTaskInstructions(bean);
  }

  /// 修改任务指令
  void updateTaskInstructions(EmergencyTaskInstructionsEntity bean) async {
    // 获取流程列表接口
    final res = await HttpUtil().generatePostRequest(Interface.updateTaskInstructions, data: bean.toJson()).execute();
    res.onSuccess((result) {
      if (result.data != null) {}
    });
  }
}
